package com.ly.emp.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ly.common.core.bean.R;
import com.ly.common.core.bean.dto.CommonBatchUpdateDTO;
import com.ly.common.core.utils.BaseUtils;
import com.ly.emp.bean.dto.HubTeamDTO;
import com.ly.emp.constant.EmpConstants;
import com.ly.emp.constant.EmpErrorConstants;
import com.ly.emp.entity.HubTeamMonitoring;
import com.ly.emp.mapper.HubTeamMonitoringMapper;
import com.ly.emp.service.IHubTeamMonitoringService;
import com.ly.emp.util.TeamUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * 监控-季年度-impl
 *
 * @author luoyun
 * @Date: 2025-05-20
 */
@Service
public class HubTeamMonitoringServiceImpl extends ServiceImpl<HubTeamMonitoringMapper, HubTeamMonitoring> implements IHubTeamMonitoringService {

    @Override
    public R getId(String id) {
        HubTeamMonitoring entity = this.getById(id);
        if (entity == null) {
            return R.fail("未找到对应数据");
        }
        return R.ok(entity);
    }

    @Override
    public R getList(HubTeamMonitoring entity) {
        List<HubTeamMonitoring> list = this.list(
                new LambdaQueryWrapper<HubTeamMonitoring>()
                        .orderByAsc(HubTeamMonitoring::getDisplayOrder)
        );
        return R.ok(list);
    }

    @Override
    public R getPage(HubTeamMonitoring entity, Page<HubTeamMonitoring> page) {
        String suitUnit = BaseUtils.getSuitUnit();
        String periodCode = entity.getPeriodCode();
        List<String> list = new ArrayList<>();
        if (StrUtil.isNotBlank(entity.getOrgCode())) {
            String[] split = entity.getOrgCode().split(",");
            list = Arrays.asList(split);
        }
        if (StrUtil.isBlank(suitUnit)) {
            return R.fail(EmpErrorConstants.NO_AUTH_DATA);
        }
        if (StrUtil.isBlank(periodCode)) {
            return R.fail(EmpErrorConstants.NO_PERIOD);
        }
        IPage<HubTeamMonitoring> pageList = this.page(page,
                new LambdaQueryWrapper<HubTeamMonitoring>()
                        .eq(HubTeamMonitoring::getPeriodCode, periodCode)
                        .in(CollUtil.isNotEmpty(list), HubTeamMonitoring::getOrgCode, list)
                        .eq(StrUtil.isNotBlank(entity.getStatus()), HubTeamMonitoring::getStatus, entity.getStatus())
                        .eq(HubTeamMonitoring::getSuitUnit, suitUnit)
                        .orderByAsc(HubTeamMonitoring::getDisplayOrder));
        return R.ok(pageList);
    }

    @Override
    public R add(HubTeamMonitoring entity) {
        boolean flag = this.save(entity);
        return flag ? R.ok(true, "创建成功") : R.fail("创建失败");
    }

    @Override
    public R edit(HubTeamMonitoring entity) {
        boolean flag = this.updateById(entity);
        return flag ? R.ok(true, "修改成功") : R.fail("修改失败");
    }

    @Override
    public R delete(String id) {
        boolean flag = this.removeById(id);
        return flag ? R.ok(true, "删除成功") : R.fail("删除失败");
    }

    @Override
    public R deleteBatch(String ids) {
        boolean flag = this.removeByIds(Arrays.asList(ids.split(",")));
        return flag ? R.ok(true, "批量删除成功") : R.fail("批量删除失败");
    }

    @Override
    public R updateBatch(CommonBatchUpdateDTO<HubTeamMonitoring> dto) {
        List<HubTeamMonitoring> datas = dto.getDatas();
        boolean flag = this.saveOrUpdateBatch(datas);
        return flag ? R.ok(true, "批量修改成功") : R.fail("批量修改失败");
    }

    @Override
    public HubTeamMonitoring getHubTeamMonitoringByParams(HubTeamDTO dto) {
        String periodCode = dto.getPeriodCode();
        String orgCode = dto.getOrgCode();
        List<HubTeamMonitoring> list = this.list(
                new LambdaQueryWrapper<HubTeamMonitoring>()
                        .eq(StrUtil.isNotBlank(periodCode), HubTeamMonitoring::getPeriodCode, periodCode)
                        .eq(StrUtil.isNotBlank(orgCode), HubTeamMonitoring::getOrgCode, orgCode)
        );
        if (CollUtil.isNotEmpty(list)) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public R getMonitoringStatus(HubTeamDTO dto) {
        // 查看班组的启动状态
        HubTeamMonitoring monitoring = this.getHubTeamMonitoringByParams(dto);
        // 季度未启动，则不允许操作
        if (monitoring == null && "2".equals(TeamUtils.getPeriodTypeByCode(dto.getPeriodCode()))) {
            return R.ok("2");
        }
        //（已提交、审核通过、已发布，这三个状态，不能再录入数据）: 10已汇总 15已退回 20已提交 30已审核 100已发布
        if (monitoring != null){
            String status = monitoring.getStatus();
            if (EmpConstants.RESULT_STATUS_PUBLISHED.equals(status) || EmpConstants.RESULT_STATUS_SUBMIT.equals(status) || EmpConstants.RESULT_STATUS_PASS.equals(status)) {
                return R.ok("2");
            }
        }
        return R.ok("1");
    }
}
